package com.njchy.statistics.controller;


import com.njchy.statistics.entity.dto.MatchIncomingPointsDTO;
import com.njchy.statistics.entity.dto.PolygonListParamDTO;
import com.njchy.statistics.entity.dto.RiverSumDTO;
import com.njchy.statistics.mapper.AllMapper;
import com.njchy.statistics.service.AllServices;
import com.njchy.statistics.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * 控制层。
 *
 * @author 测绘院
 * @since 2025年08月25日
 */
@RestController
@RequestMapping("/sta")
@Api(tags = "数据统计模块")
public class StaController {

    @Autowired
    private AllServices allServices;

    @Autowired
    private AllMapper allMapper;


    /**
     * 区间内面积和体积计算
     * 仅统计fkid相同且表一value - 表二value > 0的数据
     * 返回格式：[{range: "0--0.06", Area: "xx", Volume: "xx"}, ...]
     *
     * @param tableNameOne tableNameTwo
     * @return List<Map<String, String>>
     */
    @GetMapping("/areaStatistic")
    @ApiOperation("区间内面积和体积计算")
    public CommonResult getAreaStatistic(
            @RequestParam(required = true) String tableNameOne,
            @RequestParam(required = true) String tableNameTwo) {
        List<Map<String, String>> areaStatistic = allServices.getAreaStatistic(tableNameOne, tableNameTwo);
        return CommonResult.success(areaStatistic);
    }


    /**
     * 单个点位的查询数据匹配
     *
     * @param tableNameOne tableNameTwo
     * @return List<Map<String, String>>
     */
    @GetMapping("/singleTableData")
    @ApiOperation("单个点位的查询数据匹配")
    public CommonResult  singleTableData(
            @RequestParam(required = true) String tableNameOne,
            @RequestParam(required = true)String point) {
        List<Map<String, Object>> areaStatistic = allServices.singleTableData(tableNameOne, point);
        return CommonResult.success(areaStatistic);
    }


    /**
     * 面积和体积计算
     * 仅统计fkid相同且表一value - 表二value > 0的数据
     * 返回格式：{TotalArea: "xx", totalVolume: "xx"}
     *
     * @param tableNameOne tableNameTwo
     * @return Map<String, String>
     */
    @GetMapping("/volumeStatistic")
    @ApiOperation("面积和体积计算")
    public CommonResult getVolumeStatistic(
            @RequestParam(required = true) String tableNameOne,
            @RequestParam(required = true) String tableNameTwo) {
        Map<String, String> oneSta = allServices.getVolumeStatistic(tableNameOne, tableNameTwo);
        return CommonResult.success(oneSta);
    }


    /**
     * 根据动态表名和点列表查询匹配结果
     *
     * @param dto
     * @return 匹配结果
     */
    @PostMapping("/matchIncomingPoints")
    @ApiOperation("根据动态表名和点列表查询匹配结果")
    public CommonResult<Map<String, List<Map<String, String>>>> getMatchIncomingPoints(
            @Valid @RequestBody MatchIncomingPointsDTO dto) {
        Map<String, List<Map<String, String>>> resultData = allServices.getMatchIncomingPoints(dto);
        return CommonResult.success(resultData);
    }

    /**
     * 河道数据统计
     *
     * @return
     */
    @GetMapping("/riverSum")
    @ApiOperation("河道数据统计")
    public CommonResult<RiverSumDTO> getRiverSum() {
        RiverSumDTO sumDTO = allServices.getRiverDataSum();
        return CommonResult.success(sumDTO, "数据统计成功");
    }


    /**
     * 获取统计列表
     */
    @GetMapping("/list")
    @ApiOperation("获取统计列表")
    public CommonResult selecRivertList() {
        return CommonResult.success(allServices.selecRivertList());
    }


    /**
     * 范围面数据统计（查询范围内对象，统计Value的累加值*0.09算整体的差值，统计Value的个数*0.09）
     */
    @PostMapping("/areaStatistic")
    @ApiOperation("范围面数据统计")
    public CommonResult areaStatistic(@RequestBody PolygonListParamDTO dto) {
        return CommonResult.success(allMapper.areaStatisticDifference(dto));
    }


}
